package com.zyb.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.eduservice.entity.EduCourse;
import com.zyb.eduservice.entity.EduCourseDescription;
import com.zyb.eduservice.entity.EduTeacher;
import com.zyb.eduservice.entity.frontvo.CourseFrontVo;
import com.zyb.eduservice.entity.frontvo.CourseWebVo;
import com.zyb.eduservice.entity.vo.CourseInfoVo;
import com.zyb.eduservice.entity.vo.CoursePublishVo;
import com.zyb.eduservice.entity.vo.CourseQuery;
import com.zyb.eduservice.mapper.EduCourseMapper;
import com.zyb.eduservice.service.EduChapterService;
import com.zyb.eduservice.service.EduCourseDescriptionService;
import com.zyb.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyb.eduservice.service.EduVideoService;
import com.zyb.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testJava
 * @since 2022-04-02
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Resource
    private EduCourseDescriptionService descriptionService;
    @Resource
    private EduVideoService videoService;
    @Resource
    private EduChapterService chapterService;

    @Override
    //添加课程基本信息方法
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {
        //1 向课程表添加课程基本信息
        //CourseInfoVo对象转换eduCourse对象
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0) {
            throw new GuliException(20001, "添加课程失败");
        }

        //获取添加之后课程id
        String cid = eduCourse.getId();
        //2 向课程简介表添加课程简介
        //edu_course_description
        EduCourseDescription description = new EduCourseDescription();
        description.setDescription(courseInfoVo.getDescription());
        //设置描述id就是课程id
        description.setId(cid);
        descriptionService.save(description);

        return cid;
    }

    @Override
    //根据ID查询课程
    public CourseInfoVo getCourseInfo(String courseId) {
        //1 查询课程表
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse, courseInfoVo);

        //2 查询描述表
        EduCourseDescription courseDescription = descriptionService.getById(courseId);
        courseInfoVo.setDescription(courseDescription.getDescription());

        return courseInfoVo;
    }

    @Override
    //修改课程基本信息
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int i = baseMapper.updateById(eduCourse);

        if (i == 0) {
            throw new GuliException(20001, "修改失败");
        }

        //修改描述表
        EduCourseDescription courseDescription = new EduCourseDescription();
        BeanUtils.copyProperties(courseInfoVo, courseDescription);
        descriptionService.updateById(courseDescription);
    }

    @Override
    public CoursePublishVo getAllCourseInfo(String courseId) {
        CoursePublishVo allCourseInfo = baseMapper.getAllCourseInfo(courseId);
        return allCourseInfo;
    }

    @Override
    public IPage<EduCourse> pageCourseCondition(Long current, Long limit, CourseQuery courseQuery) {
        IPage<EduCourse> iPage = new Page<>(current, limit);
        //构造条件
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        String title = courseQuery.getTitle();
        String status = courseQuery.getStatus();
        String begin = courseQuery.getBegin();
        String end = courseQuery.getEnd();
        //多条件进行查询
        // mybatis学过 动态sql
        if (!StringUtils.isEmpty(title)) {
            wrapper.like("title", title);
        }
        if (!StringUtils.isEmpty(status)) {
            wrapper.eq("status", status);
        }
        if (!StringUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            wrapper.le("gmt_modified", end);
        }
        //进行排序
        wrapper.orderByDesc("gmt_create");
        baseMapper.selectPage(iPage, wrapper);
        return iPage;
    }

    @Override
    public void removeCourse(String courseId) {
        //1 根据课程id删除小节
        videoService.removeVideoByCourseId(courseId);
        //2 根据课程id删除章节
        chapterService.removeChapter(courseId);
        //3 根据课程id删除描述
        descriptionService.removeById(courseId);
        //4 根据课程id删除课程本身
        int i = baseMapper.deleteById(courseId);

        if (i == 0) {
            throw new GuliException(20001, "删除失败");
        }
    }

    //@Cacheable(key = "'selectIndexList'",value = "course")
    @Override
    //查询前8条课程
    public List<EduCourse> cmsQueryCourse() {
        QueryWrapper<EduCourse> wrapperCourse = new QueryWrapper<>();
        wrapperCourse.orderByDesc("id");
        wrapperCourse.last("limit 8");
        List<EduCourse> eduCoursesList = baseMapper.selectList(wrapperCourse);
        return eduCoursesList;
    }

    //条件查询带分页查询课程
    @Override
    public Map<String, Object> getCourseFrontList(Page<EduCourse> pageParam, CourseFrontVo courseFrontVo) {

        //根据讲师id查询所讲课程
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        //判断条件值是否为空，不为空拼接
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) { //一级分类
            wrapper.eq("subject_parent_id", courseFrontVo.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getSubjectId())) { //二级分类
            wrapper.eq("subject_id", courseFrontVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) { //关注度
            wrapper.orderByDesc("buy_count");
        }
        if (!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) { //最新
            wrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {//价格
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageParam, wrapper);

        List<EduCourse> records = pageParam.getRecords();
        long current = pageParam.getCurrent();
        long pages = pageParam.getPages();
        long size = pageParam.getSize();
        long total = pageParam.getTotal();
        boolean hasNext = pageParam.hasNext();//下一页
        boolean hasPrevious = pageParam.hasPrevious();//上一页

        //把分页数据获取出来，放到map集合
        Map<String, Object> map = new HashMap<>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        //map返回
        return map;
    }

    @Override
    //根据课程id，编写sql语句查询课程信息
    public CourseWebVo getBaseCourseInfo(String courseId) {
        return baseMapper.getFrontBaseCourseInfo(courseId);
    }

}
